package com.lims;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author limaosheng
 * @create 2023-10-31 17:22
 * @desc
 */
public class 汉字转拼音 {

    public static void main(String[] args) {
        String a = "广东\n" +
                "江苏\n" +
                "上海\n" +
                "浙江\n" +
                "四川\n" +
                "福建\n" +
                "湖北\n" +
                "安徽\n" +
                "湖南\n" +
                "陕西\n" +
                "广西\n" +
                "云南\n" +
                "江西\n" +
                "重庆\n" +
                "新疆\n" +
                "甘肃\n" +
                "贵州\n" +
                "海南\n" +
                "宁夏\n" +
                "青海\n" +
                "西藏\n" +
                "北京\n" +
                "河北\n" +
                "山东\n" +
                "河南\n" +
                "辽宁\n" +
                "黑龙江\n" +
                "山西\n" +
                "内蒙古\n" +
                "吉林\n" +
                "天津\n" +
                "其它\n";
        String[] arr = a.split("\n");
        int i = 1;
        for(String s : arr){
            String pinyin = toPinyin(s);
            System.out.println(pinyin + "(" + i + ",\""+s+"\"),");
            i++;
        }
    }

    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String toPinyin(String chinese) {
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr.toUpperCase();
    }
}
